package com.wing.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.system.dao.PropertyTypeDao;
import com.wing.system.model.entity.PropertyType;
import com.wing.system.model.request.PropertyTypeForm;
import com.wing.system.model.response.PropertyTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 物料与资产类型-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PropertyTypeServiceImpl extends ServiceImpl<PropertyTypeDao, PropertyType> implements PropertyTypeService {

    @Override
    public int add(PropertyTypeForm propertyTypeForm) {

        //校验

        PropertyType propertyType =new PropertyType();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PropertyType.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(propertyTypeForm, propertyType, copyOptions);
        String code=getCode();
        propertyType.setCode("M"+code);
        propertyType.setNo(Integer.valueOf(code));
        return baseMapper.insert(propertyType);
    }

    private String getCode(){
        String code="";
        Integer maxNo=baseMapper.getMaxNo();
        if (maxNo==null){
            maxNo=1;
        }else{
            maxNo+=1;
        }
        for (int i=0;i<4-maxNo.toString().length();i++){
            code+="0";
        }
        return code+maxNo.toString();
    }
    @Override
    public List<PropertyTypeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PropertyTypeVO> pageList4app(Page page, Map<String, Object> params) {
        List<PropertyTypeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PropertyTypeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
